package com.kuang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kuang.utils.ControllerLogUtils;
import com.kuang.utils.MQTTConnect;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

@Controller
@RequestMapping("/single/down/")
@Slf4j
public class ImportRemoteOpenWellController {


    @Resource
    MQTTConnect mqttConnect;
    Logger logger = Logger.getLogger(ImportRemoteOpenWellController.class);

    //2.6 服务器远程开井 cmd=remote_open_well
    @RequestMapping("ImportRemoteOpenWell")
    @ResponseBody
    public String ImportRemoteOpenWell(@RequestBody String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        Map check = check(jsonObject);
        //结果集
        Map result = new LinkedHashMap();

        if (check != null) {
            String string = MapUtils.getString(check, "500");
            result.put("success", "false");
            result.put("code", "500");
            result.put("msg", string);
            // 日志记录输出
            //log.info(JSON.toJSONString(result));
            logger.debug(JSON.toJSONString(result));
            int i = ControllerLogUtils.setErrorMsgLog("import", "/single/down/ImportRemoteOpenWell",
                    "服务器远程开井", string, json, JSON.toJSONString(result));
            return JSON.toJSONString(result);
        }

        //发送数据
        String device_code = MapUtils.getString(jsonObject, "device_code");
        try {
            boolean pub = mqttConnect.pub("/single/down/" + device_code, json);//发布主题
        } catch (MqttException e) {
            e.printStackTrace();
            result.put("success", "false");
            result.put("code", "500");
            result.put("msg", e.getMessage());
            logger.debug(JSON.toJSONString(result));
            int i = ControllerLogUtils.setErrorMsgLog("import", "/single/down/ImportRemoteOpenWell",
                    "服务器远程开井", e.getMessage(), json, JSON.toJSONString(result));
            return JSON.toJSONString(result);
        }

        result.put("success", "true");
        result.put("code", "200");
        result.put("msg", "处理成功！");
        logger.info(JSON.toJSONString(result));

        int i = ControllerLogUtils.setMsgLog("import", "/single/down/ImportRemoteOpenWell",
                "服务器远程开井", "处理成功！", json, JSON.toJSONString(result));
        return JSON.toJSONString(result);
    }

    public Map check(JSONObject jsonMap) {
        Map error = new LinkedHashMap();
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "device_code", ""))) {
            error.put("500", "设备编码未填写");
            return error;
        }

        String cmd = MapUtils.getString(jsonMap, "cmd", "");
        if (StringUtils.isBlank(cmd)) {
            error.put("500", "执行编号未填写");
            return error;
        }
        if (!StringUtils.equals(cmd, "6")) {
            error.put("500", "服务器远程开井执行编码错误");
            return error;
        }
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "ver", ""))) {
            error.put("500", "消息版本号未填写");
            return error;
        }

        JSONObject data = jsonMap.getJSONObject("data");
        if (data == null) {
            error.put("500", "data对象未填写");
            return error;
        }

        if (StringUtils.isBlank(MapUtils.getString(data, "user_id_card", ""))) {
            error.put("500", "用户身份证号未填写");
            return error;
        }
        Double cMoney = MapUtils.getDouble(data, "user_balance");
        if (cMoney == null) {
            error.put("500", "用户剩余金额未填写");
            return error;
        }
        return null;
    }


}
